package twoPointers;

//https://leetcode.cn/problems/duplicate-zeros/description/
public class l_1089 {
    public void duplicateZeros(int[] arr) {
        int cur = 0, dest = -1, n = arr.length;
        //找到从右向左的起始点
        while(cur < n) {
            if(arr[cur] == 0) dest += 2;
            else ++dest;
            if(dest >= n - 1) break;
            ++cur;
        }
        //边界处理,比如[0,0,3,0,5,7]
        if(dest == n) {
            --cur;
            dest -= 2;
            arr[n - 1] = 0;
        }
        //从右向左
        while(cur >= 0) {
            if(arr[cur] != 0) arr[dest--] = arr[cur--];
            else {
                arr[dest--] = 0;
                arr[dest--] = 0;
                --cur;
            }
        }
    }
}
